<script setup lang="ts">
import { uploadImg } from '@/services/consult';
import type { UploaderAfterRead } from 'vant/lib/uploader/types';
import { ref } from 'vue'
import type { Image } from '@/types/consult'

// 发送图文信息的禁用状态
defineProps<{
  disabled: boolean
}>()

// 由于当前组件中并没有socket实例，所以发送信息的动作得交给父组件
const emit = defineEmits<{
  // 发送文本信息的事件
  (e: 'send-text', text: string): void
  // 发送图片信息的事件
  (e: 'send-image', img: Image): void
}>()

// 发送文本信息
const text = ref('')
const sendText = () => {
  if (text.value.trim().length !== 0) {
    emit('send-text', text.value)
    text.value = ''
  }
}

// 发送图片
const sendImage: UploaderAfterRead = async (data) => {
  if (Array.isArray(data)) return
  if (!data.file) return
  const res = await uploadImg(data.file)
  emit('send-image', res.data)
}


</script>

<template>
  <div class="room-action">
    <van-field type="text" class="input" :border="false" placeholder="问医生" autocomplete="off" :disabled="disabled"
      v-model="text" @keyup.enter="sendText"></van-field>
    <van-uploader :preview-image="false" :disabled="disabled" :after-read="sendImage">
      <cp-icon name="consult-img" />
    </van-uploader>
  </div>
</template>

<style lang="scss" scoped>
.room-action {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 60px;
  background-color: #fff;
  display: flex;
  align-items: center;
  padding: 0 16px;
  z-index: 1;
  box-sizing: border-box;

  .input {
    background-color: var(--cp-bg);
    border: none;
    border-radius: 25px;
    margin-right: 10px;
    padding: 8px 15px;
  }

  .cp-icon {
    width: 24px;
    height: 24px;
  }
}
</style>